package com.song.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.mapper.TeamMapper;
import com.song.pojo.Team;
import com.song.pojo.TeamExample;
import com.song.pojo.TeamQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**球队的逻辑层
 * @author song
 */
@Service
public class TeamService {
    @Resource
    private TeamMapper teamMapper;

    /**
     *  分页查询（模糊条件）
     * @param pageNum 当前页数
     * @param pageSize 每页数据条数
     * @param vo 模糊与条件查询分装实体类
     * @return 查询到的所有信息
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public PageInfo<Team> queryTeams(Integer pageNum, Integer pageSize, TeamQueryVO vo){
        TeamExample example = new TeamExample();
        TeamExample.Criteria criteria = example.createCriteria();
        if (pageNum == null || pageNum < 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 0){
            pageSize = 5;
        }
        //按条件模糊查询
        if (vo.getTeamName() != null&&!"".equals(vo.getTeamName().trim())){
            criteria.andTeamNameLike("%"+vo.getTeamName().trim()+"%");
        }
        if (vo.getChineseName() != null&&!"".equals(vo.getChineseName().trim())){
            criteria.andChineseNameLike("%"+vo.getChineseName().trim()+"%");
        }
        if (vo.getCoach() != null&&!"".equals(vo.getCoach().trim())){
            criteria.andCoachLike("%"+vo.getCoach().trim()+"%");
        }
        //按球队创建时间查询
        if (vo.getBeginDate() != null){
            criteria.andCreateTimeGreaterThanOrEqualTo(vo.getBeginDate());
        }
        if (vo.getEndDate() != null){
            criteria.andCreateTimeLessThanOrEqualTo(vo.getEndDate());
        }
        //安球队赛区查询
        if (vo.getArea() != null && vo.getArea() != -1){
            criteria.andAreaEqualTo(vo.getArea());
        }
        //分页查询工具，只对紧邻下一条管用
        PageHelper.startPage(pageNum,pageSize);
        List<Team> teamList = teamMapper.selectByExample(example);
        return new PageInfo<>(teamList);

    }

    /**
     * 新增球队
     * @param team 新增球队对象
     * @return 添加成功 返回true 否则false
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Boolean insertTeam(Team team){
        team.setIsDel(0);
        int insert = teamMapper.insertSelective(team);
        if (insert == 1){
            return true;
        }
        return false;
    }

    /**
     * 通过ID查询球队信息
     * @param teamId 球队ID
     * @return 球队对象
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Team queryByTeamId(Integer teamId){
        return teamMapper.selectByPrimaryKey(teamId);
    }

    /**
     * 更新球队信息
     * @param team 更新球队对象
     * @return 更新成功 返回true 否则false
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer updateTeam(Team team){
        return teamMapper.updateByPrimaryKeySelective(team);
    }

    /**
     * 根据主键删除
     * @param
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer deleteTeam(Team team){
        return teamMapper.updateByPrimaryKeySelective(team);
    }
}
